const app = getApp();
var config = app.globalData.config;
Page({

	data: {
		checked: false,
		mobile: '', //手机号
		verify: '', //验证码
		login_step: 0
	},

	onLoad(options) {

	},

	//获取基本信息
	getUserProfile(e) {
		if (this.data.checked == false) {
			wx.showToast({
				title: '请同意用户协议',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		wx.getUserProfile({
			desc: '用于完善会员资料',
			success: (res) => {
				this.setData({
					userInfo: res.userInfo
				})
				this.doLogin(res.userInfo);
			}
		})
	},

	//登录
	doLogin: function(userInfo) {
		var that = this;
		wx.login({
			success: function(res) {
				//获取登录的临时凭证
				//调用后端，获取微信的session_key,secret
				wx.request({
					url: config.wx_login,
					method: "post",
					header: {
						"Content-Type": "application/json",
					},
					data: {
						code: res.code,
						rawData: userInfo,
						versions: config.versions
					},
					success: function(result) {
						that.setData({
							wxapp_info: result.data.data,
							login_step: 1
						})
					}
				})
			}
		})
	},

	mobileChange: function(e) {
		this.setData({
			mobile: e.detail.value
		})
	},

	verifyChange: function(e) {
		this.setData({
			verify: e.detail.value
		})
	},

	sendSms: function() {
		if (this.data.mobile == '') {
			wx.showToast({
				title: '请输入手机号',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		if (!(/^1[34578]\d{9}$/.test(this.data.mobile))) {
			wx.showToast({
				title: '手机号格式不正确',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		wx.request({
			url: config.send_sms,
			method: 'post',
			header: {
				"Content-Type": "application/json",
			},
			data: {
				mobile: this.data.mobile
			},
			success: function(result) {
				wx.showToast({
					title: result.data.msg,
					icon: 'none',
					duration: 1000,
					mask: true
				});
			}
		})
	},

	//登录
	toLogin: function() {
		if (this.data.checked == false) {
			wx.showToast({
				title: '请同意用户协议',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		if (this.data.mobile == '') {
			wx.showToast({
				title: '请输入手机号',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		if (!(/^1[34578]\d{9}$/.test(this.data.mobile))) {
			wx.showToast({
				title: '手机号格式不正确',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		if (this.data.verify == '') {
			wx.showToast({
				title: '请输入验证码',
				icon: 'none',
				duration: 1000,
				mask: true
			});
			return;
		}
		wx.request({
			url: config.verify_sms,
			method: 'post',
			header: {
				"Content-Type": "application/json",
			},
			data: {
				mobile: this.data.mobile,
				captcha: this.data.verify,
				openid: this.data.wxapp_info.openid,
				nickName: this.data.userInfo.nickName,
				avatarUrl: this.data.userInfo.avatarUrl,
				gender: this.data.userInfo.gender,
			},
			success: function(result) {
				if (result.data.code == 1) {
					var data = result.data.data;
					wx.setStorageSync('userInfo', data.user_info);
					app.globalData.userInfo = data.user_info;
					wx.setStorageSync('token', data.token)
					app.globalData.token = data.token;

					wx.showToast({
						title: '授权成功', // 标题
						icon: 'success', // 图标类型，默认success
						duration: 1000 // 提示窗停留时间，默认1500ms
					})
					
					var checkRole = data.user_info.group_id;
					if(checkRole==1 || checkRole=='1'){
						wx.reLaunch({
							url: '../index/index',
						})
					}else{
						wx.reLaunch({
							url: '../../teacher/index/tindex',
						})
					}
				} else {
					wx.showToast({
						title: result.data.msg,
						icon: 'none',
						duration: 1000,
						mask: true
					});
				}
			}
		})
	},

	//勾选用户协议
	protocol_check(e) {
		let _check = e.detail.value;
		if (_check.length > 0) {
			this.setData({
				checked: true
			})
		} else {
			this.setData({
				checked: false
			})
		}
	}

})
